{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "10yLPb9Fneh0"
      },
      "source": [
        "# FarmerGPT - Build Chat with PDF using lancedb"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "xFHTAtayosS-"
      },
      "source": [
        "We utilized this PDF: Dataset/FARM5CROPS_farmergpt.pdf,\n",
        " which contains crop variety information for sugarcane, turmeric, bamboo, cashew nuts, and more.\n",
        " This is a sample project designed to demonstrate how to build an application using LanceDB and LangChain\n",
        "The use case and prompts can be customized as needed to suit specific requirements"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "eXsCZEUvb39u"
      },
      "source": [
        "Import pacages"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "EaKlPN9u6bnc",
        "outputId": "5e2e2388-13c2-448c-fc70-c1cdab441a35"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: langchain in /usr/local/lib/python3.11/dist-packages (0.3.27)\n",
            "Collecting lancedb\n",
            "  Downloading lancedb-0.24.2-cp39-abi3-manylinux_2_28_x86_64.whl.metadata (4.4 kB)\n",
            "Requirement already satisfied: langchain-core<1.0.0,>=0.3.72 in /usr/local/lib/python3.11/dist-packages (from langchain) (0.3.72)\n",
            "Requirement already satisfied: langchain-text-splitters<1.0.0,>=0.3.9 in /usr/local/lib/python3.11/dist-packages (from langchain) (0.3.9)\n",
            "Requirement already satisfied: langsmith>=0.1.17 in /usr/local/lib/python3.11/dist-packages (from langchain) (0.4.8)\n",
            "Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /usr/local/lib/python3.11/dist-packages (from langchain) (2.11.7)\n",
            "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.11/dist-packages (from langchain) (2.0.41)\n",
            "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.11/dist-packages (from langchain) (2.32.3)\n",
            "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.11/dist-packages (from langchain) (6.0.2)\n",
            "Collecting deprecation (from lancedb)\n",
            "  Downloading deprecation-2.1.0-py2.py3-none-any.whl.metadata (4.6 kB)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from lancedb) (2.0.2)\n",
            "Collecting overrides>=0.7 (from lancedb)\n",
            "  Downloading overrides-7.7.0-py3-none-any.whl.metadata (5.8 kB)\n",
            "Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from lancedb) (25.0)\n",
            "Requirement already satisfied: pyarrow>=16 in /usr/local/lib/python3.11/dist-packages (from lancedb) (18.1.0)\n",
            "Requirement already satisfied: tqdm>=4.27.0 in /usr/local/lib/python3.11/dist-packages (from lancedb) (4.67.1)\n",
            "Requirement already satisfied: tenacity!=8.4.0,<10.0.0,>=8.1.0 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.72->langchain) (8.5.0)\n",
            "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.72->langchain) (1.33)\n",
            "Requirement already satisfied: typing-extensions>=4.7 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.72->langchain) (4.14.1)\n",
            "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.1.17->langchain) (0.28.1)\n",
            "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.1.17->langchain) (3.11.0)\n",
            "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.1.17->langchain) (1.0.0)\n",
            "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.1.17->langchain) (0.23.0)\n",
            "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.7.0)\n",
            "Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/lib/python3.11/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain) (2.33.2)\n",
            "Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain) (0.4.1)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain) (3.4.2)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain) (3.10)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain) (2.5.0)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain) (2025.7.14)\n",
            "Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.11/dist-packages (from SQLAlchemy<3,>=1.4->langchain) (3.2.3)\n",
            "Requirement already satisfied: anyio in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (4.9.0)\n",
            "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (1.0.9)\n",
            "Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (0.16.0)\n",
            "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.11/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.72->langchain) (3.0.0)\n",
            "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.11/dist-packages (from anyio->httpx<1,>=0.23.0->langsmith>=0.1.17->langchain) (1.3.1)\n",
            "Downloading lancedb-0.24.2-cp39-abi3-manylinux_2_28_x86_64.whl (34.8 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m34.8/34.8 MB\u001b[0m \u001b[31m63.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading overrides-7.7.0-py3-none-any.whl (17 kB)\n",
            "Downloading deprecation-2.1.0-py2.py3-none-any.whl (11 kB)\n",
            "Installing collected packages: overrides, deprecation, lancedb\n",
            "Successfully installed deprecation-2.1.0 lancedb-0.24.2 overrides-7.7.0\n",
            "Collecting tantivy\n",
            "  Downloading tantivy-0.24.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (1.4 kB)\n",
            "Downloading tantivy-0.24.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.1 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.1/4.1 MB\u001b[0m \u001b[31m31.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hInstalling collected packages: tantivy\n",
            "Successfully installed tantivy-0.24.0\n",
            "Collecting langchain-openai\n",
            "  Downloading langchain_openai-0.3.28-py3-none-any.whl.metadata (2.3 kB)\n",
            "Collecting langchain-community\n",
            "  Downloading langchain_community-0.3.27-py3-none-any.whl.metadata (2.9 kB)\n",
            "Collecting pypdf\n",
            "  Downloading pypdf-5.9.0-py3-none-any.whl.metadata (7.1 kB)\n",
            "Requirement already satisfied: langchain-core<1.0.0,>=0.3.68 in /usr/local/lib/python3.11/dist-packages (from langchain-openai) (0.3.72)\n",
            "Requirement already satisfied: openai<2.0.0,>=1.86.0 in /usr/local/lib/python3.11/dist-packages (from langchain-openai) (1.97.1)\n",
            "Requirement already satisfied: tiktoken<1,>=0.7 in /usr/local/lib/python3.11/dist-packages (from langchain-openai) (0.9.0)\n",
            "Requirement already satisfied: langchain<1.0.0,>=0.3.26 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (0.3.27)\n",
            "Requirement already satisfied: SQLAlchemy<3,>=1.4 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (2.0.41)\n",
            "Requirement already satisfied: requests<3,>=2 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (2.32.3)\n",
            "Requirement already satisfied: PyYAML>=5.3 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (6.0.2)\n",
            "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (3.12.14)\n",
            "Requirement already satisfied: tenacity!=8.4.0,<10,>=8.1.0 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (8.5.0)\n",
            "Collecting dataclasses-json<0.7,>=0.5.7 (from langchain-community)\n",
            "  Downloading dataclasses_json-0.6.7-py3-none-any.whl.metadata (25 kB)\n",
            "Collecting pydantic-settings<3.0.0,>=2.4.0 (from langchain-community)\n",
            "  Downloading pydantic_settings-2.10.1-py3-none-any.whl.metadata (3.4 kB)\n",
            "Requirement already satisfied: langsmith>=0.1.125 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (0.4.8)\n",
            "Collecting httpx-sse<1.0.0,>=0.4.0 (from langchain-community)\n",
            "  Downloading httpx_sse-0.4.1-py3-none-any.whl.metadata (9.4 kB)\n",
            "Requirement already satisfied: numpy>=1.26.2 in /usr/local/lib/python3.11/dist-packages (from langchain-community) (2.0.2)\n",
            "Requirement already satisfied: aiohappyeyeballs>=2.5.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (2.6.1)\n",
            "Requirement already satisfied: aiosignal>=1.4.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.4.0)\n",
            "Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (25.3.0)\n",
            "Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.7.0)\n",
            "Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (6.6.3)\n",
            "Requirement already satisfied: propcache>=0.2.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (0.3.2)\n",
            "Requirement already satisfied: yarl<2.0,>=1.17.0 in /usr/local/lib/python3.11/dist-packages (from aiohttp<4.0.0,>=3.8.3->langchain-community) (1.20.1)\n",
            "Collecting marshmallow<4.0.0,>=3.18.0 (from dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
            "  Downloading marshmallow-3.26.1-py3-none-any.whl.metadata (7.3 kB)\n",
            "Collecting typing-inspect<1,>=0.4.0 (from dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
            "  Downloading typing_inspect-0.9.0-py3-none-any.whl.metadata (1.5 kB)\n",
            "Requirement already satisfied: langchain-text-splitters<1.0.0,>=0.3.9 in /usr/local/lib/python3.11/dist-packages (from langchain<1.0.0,>=0.3.26->langchain-community) (0.3.9)\n",
            "Requirement already satisfied: pydantic<3.0.0,>=2.7.4 in /usr/local/lib/python3.11/dist-packages (from langchain<1.0.0,>=0.3.26->langchain-community) (2.11.7)\n",
            "Requirement already satisfied: jsonpatch<2.0,>=1.33 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.68->langchain-openai) (1.33)\n",
            "Requirement already satisfied: typing-extensions>=4.7 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.68->langchain-openai) (4.14.1)\n",
            "Requirement already satisfied: packaging>=23.2 in /usr/local/lib/python3.11/dist-packages (from langchain-core<1.0.0,>=0.3.68->langchain-openai) (25.0)\n",
            "Requirement already satisfied: httpx<1,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.1.125->langchain-community) (0.28.1)\n",
            "Requirement already satisfied: orjson<4.0.0,>=3.9.14 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.1.125->langchain-community) (3.11.0)\n",
            "Requirement already satisfied: requests-toolbelt<2.0.0,>=1.0.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.1.125->langchain-community) (1.0.0)\n",
            "Requirement already satisfied: zstandard<0.24.0,>=0.23.0 in /usr/local/lib/python3.11/dist-packages (from langsmith>=0.1.125->langchain-community) (0.23.0)\n",
            "Requirement already satisfied: anyio<5,>=3.5.0 in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (4.9.0)\n",
            "Requirement already satisfied: distro<2,>=1.7.0 in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (1.9.0)\n",
            "Requirement already satisfied: jiter<1,>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (0.10.0)\n",
            "Requirement already satisfied: sniffio in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (1.3.1)\n",
            "Requirement already satisfied: tqdm>4 in /usr/local/lib/python3.11/dist-packages (from openai<2.0.0,>=1.86.0->langchain-openai) (4.67.1)\n",
            "Collecting python-dotenv>=0.21.0 (from pydantic-settings<3.0.0,>=2.4.0->langchain-community)\n",
            "  Downloading python_dotenv-1.1.1-py3-none-any.whl.metadata (24 kB)\n",
            "Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from pydantic-settings<3.0.0,>=2.4.0->langchain-community) (0.4.1)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain-community) (3.4.2)\n",
            "Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain-community) (3.10)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain-community) (2.5.0)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests<3,>=2->langchain-community) (2025.7.14)\n",
            "Requirement already satisfied: greenlet>=1 in /usr/local/lib/python3.11/dist-packages (from SQLAlchemy<3,>=1.4->langchain-community) (3.2.3)\n",
            "Requirement already satisfied: regex>=2022.1.18 in /usr/local/lib/python3.11/dist-packages (from tiktoken<1,>=0.7->langchain-openai) (2024.11.6)\n",
            "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx<1,>=0.23.0->langsmith>=0.1.125->langchain-community) (1.0.9)\n",
            "Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx<1,>=0.23.0->langsmith>=0.1.125->langchain-community) (0.16.0)\n",
            "Requirement already satisfied: jsonpointer>=1.9 in /usr/local/lib/python3.11/dist-packages (from jsonpatch<2.0,>=1.33->langchain-core<1.0.0,>=0.3.68->langchain-openai) (3.0.0)\n",
            "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain<1.0.0,>=0.3.26->langchain-community) (0.7.0)\n",
            "Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/lib/python3.11/dist-packages (from pydantic<3.0.0,>=2.7.4->langchain<1.0.0,>=0.3.26->langchain-community) (2.33.2)\n",
            "Collecting mypy-extensions>=0.3.0 (from typing-inspect<1,>=0.4.0->dataclasses-json<0.7,>=0.5.7->langchain-community)\n",
            "  Downloading mypy_extensions-1.1.0-py3-none-any.whl.metadata (1.1 kB)\n",
            "Downloading langchain_openai-0.3.28-py3-none-any.whl (70 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.6/70.6 kB\u001b[0m \u001b[31m5.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading langchain_community-0.3.27-py3-none-any.whl (2.5 MB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.5/2.5 MB\u001b[0m \u001b[31m57.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading pypdf-5.9.0-py3-none-any.whl (313 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m313.2/313.2 kB\u001b[0m \u001b[31m29.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading dataclasses_json-0.6.7-py3-none-any.whl (28 kB)\n",
            "Downloading httpx_sse-0.4.1-py3-none-any.whl (8.1 kB)\n",
            "Downloading pydantic_settings-2.10.1-py3-none-any.whl (45 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m45.2/45.2 kB\u001b[0m \u001b[31m4.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading marshmallow-3.26.1-py3-none-any.whl (50 kB)\n",
            "\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m50.9/50.9 kB\u001b[0m \u001b[31m5.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25hDownloading python_dotenv-1.1.1-py3-none-any.whl (20 kB)\n",
            "Downloading typing_inspect-0.9.0-py3-none-any.whl (8.8 kB)\n",
            "Downloading mypy_extensions-1.1.0-py3-none-any.whl (5.0 kB)\n",
            "Installing collected packages: python-dotenv, pypdf, mypy-extensions, marshmallow, httpx-sse, typing-inspect, pydantic-settings, dataclasses-json, langchain-openai, langchain-community\n",
            "Successfully installed dataclasses-json-0.6.7 httpx-sse-0.4.1 langchain-community-0.3.27 langchain-openai-0.3.28 marshmallow-3.26.1 mypy-extensions-1.1.0 pydantic-settings-2.10.1 pypdf-5.9.0 python-dotenv-1.1.1 typing-inspect-0.9.0\n",
            "Requirement already satisfied: gradio in /usr/local/lib/python3.11/dist-packages (5.38.1)\n",
            "Requirement already satisfied: aiofiles<25.0,>=22.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (24.1.0)\n",
            "Requirement already satisfied: anyio<5.0,>=3.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (4.9.0)\n",
            "Requirement already satisfied: brotli>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (1.1.0)\n",
            "Requirement already satisfied: fastapi<1.0,>=0.115.2 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.116.1)\n",
            "Requirement already satisfied: ffmpy in /usr/local/lib/python3.11/dist-packages (from gradio) (0.6.1)\n",
            "Requirement already satisfied: gradio-client==1.11.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (1.11.0)\n",
            "Requirement already satisfied: groovy~=0.1 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.1.2)\n",
            "Requirement already satisfied: httpx<1.0,>=0.24.1 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.28.1)\n",
            "Requirement already satisfied: huggingface-hub>=0.28.1 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.33.5)\n",
            "Requirement already satisfied: jinja2<4.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.1.6)\n",
            "Requirement already satisfied: markupsafe<4.0,>=2.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.0.2)\n",
            "Requirement already satisfied: numpy<3.0,>=1.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.0.2)\n",
            "Requirement already satisfied: orjson~=3.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (3.11.0)\n",
            "Requirement already satisfied: packaging in /usr/local/lib/python3.11/dist-packages (from gradio) (25.0)\n",
            "Requirement already satisfied: pandas<3.0,>=1.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.2.2)\n",
            "Requirement already satisfied: pillow<12.0,>=8.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (11.3.0)\n",
            "Requirement already satisfied: pydantic<2.12,>=2.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.11.7)\n",
            "Requirement already satisfied: pydub in /usr/local/lib/python3.11/dist-packages (from gradio) (0.25.1)\n",
            "Requirement already satisfied: python-multipart>=0.0.18 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.0.20)\n",
            "Requirement already satisfied: pyyaml<7.0,>=5.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (6.0.2)\n",
            "Requirement already satisfied: ruff>=0.9.3 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.12.5)\n",
            "Requirement already satisfied: safehttpx<0.2.0,>=0.1.6 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.1.6)\n",
            "Requirement already satisfied: semantic-version~=2.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (2.10.0)\n",
            "Requirement already satisfied: starlette<1.0,>=0.40.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.47.2)\n",
            "Requirement already satisfied: tomlkit<0.14.0,>=0.12.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.13.3)\n",
            "Requirement already satisfied: typer<1.0,>=0.12 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.16.0)\n",
            "Requirement already satisfied: typing-extensions~=4.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (4.14.1)\n",
            "Requirement already satisfied: uvicorn>=0.14.0 in /usr/local/lib/python3.11/dist-packages (from gradio) (0.35.0)\n",
            "Requirement already satisfied: fsspec in /usr/local/lib/python3.11/dist-packages (from gradio-client==1.11.0->gradio) (2025.3.0)\n",
            "Requirement already satisfied: websockets<16.0,>=10.0 in /usr/local/lib/python3.11/dist-packages (from gradio-client==1.11.0->gradio) (15.0.1)\n",
            "Requirement already satisfied: idna>=2.8 in /usr/local/lib/python3.11/dist-packages (from anyio<5.0,>=3.0->gradio) (3.10)\n",
            "Requirement already satisfied: sniffio>=1.1 in /usr/local/lib/python3.11/dist-packages (from anyio<5.0,>=3.0->gradio) (1.3.1)\n",
            "Requirement already satisfied: certifi in /usr/local/lib/python3.11/dist-packages (from httpx<1.0,>=0.24.1->gradio) (2025.7.14)\n",
            "Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.11/dist-packages (from httpx<1.0,>=0.24.1->gradio) (1.0.9)\n",
            "Requirement already satisfied: h11>=0.16 in /usr/local/lib/python3.11/dist-packages (from httpcore==1.*->httpx<1.0,>=0.24.1->gradio) (0.16.0)\n",
            "Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.28.1->gradio) (3.18.0)\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.28.1->gradio) (2.32.3)\n",
            "Requirement already satisfied: tqdm>=4.42.1 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.28.1->gradio) (4.67.1)\n",
            "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in /usr/local/lib/python3.11/dist-packages (from huggingface-hub>=0.28.1->gradio) (1.1.5)\n",
            "Requirement already satisfied: python-dateutil>=2.8.2 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2.9.0.post0)\n",
            "Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2025.2)\n",
            "Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.11/dist-packages (from pandas<3.0,>=1.0->gradio) (2025.2)\n",
            "Requirement already satisfied: annotated-types>=0.6.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<2.12,>=2.0->gradio) (0.7.0)\n",
            "Requirement already satisfied: pydantic-core==2.33.2 in /usr/local/lib/python3.11/dist-packages (from pydantic<2.12,>=2.0->gradio) (2.33.2)\n",
            "Requirement already satisfied: typing-inspection>=0.4.0 in /usr/local/lib/python3.11/dist-packages (from pydantic<2.12,>=2.0->gradio) (0.4.1)\n",
            "Requirement already satisfied: click>=8.0.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (8.2.1)\n",
            "Requirement already satisfied: shellingham>=1.3.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (1.5.4)\n",
            "Requirement already satisfied: rich>=10.11.0 in /usr/local/lib/python3.11/dist-packages (from typer<1.0,>=0.12->gradio) (13.9.4)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.11/dist-packages (from python-dateutil>=2.8.2->pandas<3.0,>=1.0->gradio) (1.17.0)\n",
            "Requirement already satisfied: markdown-it-py>=2.2.0 in /usr/local/lib/python3.11/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (3.0.0)\n",
            "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /usr/local/lib/python3.11/dist-packages (from rich>=10.11.0->typer<1.0,>=0.12->gradio) (2.19.2)\n",
            "Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests->huggingface-hub>=0.28.1->gradio) (3.4.2)\n",
            "Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.11/dist-packages (from requests->huggingface-hub>=0.28.1->gradio) (2.5.0)\n",
            "Requirement already satisfied: mdurl~=0.1 in /usr/local/lib/python3.11/dist-packages (from markdown-it-py>=2.2.0->rich>=10.11.0->typer<1.0,>=0.12->gradio) (0.1.2)\n"
          ]
        }
      ],
      "source": [
        "! pip install langchain lancedb\n",
        "! pip install tantivy\n",
        "! pip install -U langchain-openai langchain-community pypdf\n",
        "! pip install gradio"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "ylj23ZpC-SG1"
      },
      "outputs": [],
      "source": [
        "# pass opeani key or use any LLM\n",
        "import os\n",
        "\n",
        "os.environ[\"OPENAI_API_KEY\"] = \"sk-proj-...\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "xtRI4pTPvahR",
        "outputId": "22788cb2-f83c-402e-e8e9-19814b1a767a"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "--2025-07-29 16:07:18--  https://raw.githubusercontent.com/lancedb/vectordb-recipes/main/examples/RAG-On-PDF/Dataset/FARM5CROPS_farmergpt.pdf\n",
            "Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...\n",
            "Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.\n",
            "HTTP request sent, awaiting response... 200 OK\n",
            "Length: 1398674 (1.3M) [application/octet-stream]\n",
            "Saving to: ‘FARM5CROPS_farmergpt.pdf.1’\n",
            "\n",
            "FARM5CROPS_farmergp 100%[===================>]   1.33M  --.-KB/s    in 0.05s   \n",
            "\n",
            "2025-07-29 16:07:18 (28.8 MB/s) - ‘FARM5CROPS_farmergpt.pdf.1’ saved [1398674/1398674]\n",
            "\n"
          ]
        }
      ],
      "source": [
        "# Download the sample pdf\n",
        "!wget https://raw.githubusercontent.com/lancedb/vectordb-recipes/main/examples/RAG-On-PDF/Dataset/FARM5CROPS_farmergpt.pdf"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "Pzs-a0Kcdokq",
        "outputId": "263eb90b-9c5e-46f9-acc5-48cc62be99c6"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Embedding stored in lancedb\n"
          ]
        },
        {
          "name": "stderr",
          "output_type": "stream",
          "text": [
            "/tmp/ipython-input-9-1109352485.py:45: LangChainDeprecationWarning: Please see the migration guide at: https://python.langchain.com/docs/versions/migrating_memory/\n",
            "  self.memory = ConversationBufferMemory(memory_key=\"chat_history\")\n"
          ]
        }
      ],
      "source": [
        "from lancedb.rerankers import LinearCombinationReranker\n",
        "from langchain_community.document_loaders import PyPDFLoader\n",
        "from langchain_community.vectorstores import LanceDB\n",
        "from langchain_openai import OpenAIEmbeddings, ChatOpenAI\n",
        "from langchain_text_splitters import CharacterTextSplitter\n",
        "from langchain_core.prompts import PromptTemplate\n",
        "from langchain_core.output_parsers import StrOutputParser\n",
        "from langchain.memory import ConversationBufferMemory\n",
        "\n",
        "\n",
        "class QueryProcessor:\n",
        "    def __init__(self, file_path, db_url=\"lancedb_temp\", table_name=\"lancedb_indic\"):\n",
        "        \"\"\"\n",
        "        Initialize the QueryProcessor with the PDF file and set up the vector store.\n",
        "\n",
        "        Parameters:\n",
        "            file_path (str): Path to the PDF file.\n",
        "            db_url (str): URI for the LanceDB vector store.\n",
        "            table_name (str): Name of the table in LanceDB.\n",
        "        \"\"\"\n",
        "        # Load and process the PDF document\n",
        "        loader = PyPDFLoader(file_path)\n",
        "        documents = loader.load()\n",
        "        text_splitter = CharacterTextSplitter()\n",
        "        self.documents = text_splitter.split_documents(documents)\n",
        "\n",
        "        # Initialize embeddings and vector store\n",
        "        embeddings = OpenAIEmbeddings()\n",
        "        self.vector_store = LanceDB(\n",
        "            uri=db_url, embedding=embeddings, table_name=table_name\n",
        "        )\n",
        "\n",
        "        # Add reranker\n",
        "        self.reranker = LinearCombinationReranker(weight=0.3)\n",
        "        self.docsearch = LanceDB.from_documents(\n",
        "            self.documents, embeddings, reranker=self.reranker\n",
        "        )\n",
        "\n",
        "        print(\"Embedding stored in lancedb\")\n",
        "        # Initialize LLM and memory\n",
        "        self.llm = ChatOpenAI(\n",
        "            model_name=\"gpt-4o\",\n",
        "            temperature=0.01,\n",
        "        )\n",
        "        self.memory = ConversationBufferMemory(memory_key=\"chat_history\")\n",
        "\n",
        "    def generate_prompt_template(\n",
        "        self, main_instructions, prompt_instructions, context_name, query\n",
        "    ):\n",
        "        \"\"\"\n",
        "        Generate a prompt template for LangChain LLM.\n",
        "\n",
        "        Parameters:\n",
        "            main_instructions (str): Main instructions for the LLM.\n",
        "            prompt_instructions (str): Additional instructions for how to use the data.\n",
        "            context_name (str): The name of the context (e.g., search results).\n",
        "            query (str): The query from the user.\n",
        "\n",
        "        Returns:\n",
        "            PromptTemplate: The generated prompt template.\n",
        "        \"\"\"\n",
        "        template = f\"\"\"{main_instructions}\n",
        "\n",
        "        {prompt_instructions}\n",
        "\n",
        "        {context_name}:\n",
        "        {{context}}\n",
        "\n",
        "        Previous Conversations:\n",
        "        {{chat_history}}\n",
        "        Human: {query}\n",
        "        Chatbot:\"\"\"\n",
        "        return PromptTemplate(\n",
        "            template=template, input_variables=[\"context\", \"chat_history\"]\n",
        "        )\n",
        "\n",
        "    def get_answer(self, query):\n",
        "        \"\"\"\n",
        "        Process a query and return the answer based on the preloaded PDF.\n",
        "\n",
        "        Parameters:\n",
        "            query (str): The user's query.\n",
        "\n",
        "        Returns:\n",
        "            str: The answer to the query.\n",
        "        \"\"\"\n",
        "        # Perform similarity search\n",
        "        docs = self.docsearch.similarity_search_with_relevance_scores(query)\n",
        "\n",
        "        # Generate a prompt\n",
        "        prompt = self.generate_prompt_template(\n",
        "            main_instructions=\"Act as a knowledgeable assistant. Answer the query comprehensively and concisely based on the provided content.\",\n",
        "            prompt_instructions=(\n",
        "                \"Focus on extracting the most relevant and accurate information from the context. \"\n",
        "                \"Prioritize clarity, conciseness, and detail in your response. \"\n",
        "                \"When summarizing, ensure key points are highlighted without losing important nuances. \"\n",
        "                \"If the context is insufficient to fully address the query, acknowledge the limitation clearly.\"\n",
        "            ),\n",
        "            context_name=\"PDF Content\",\n",
        "            query=query,\n",
        "        )\n",
        "\n",
        "        # Create the LangChain pipeline\n",
        "        chain = prompt | self.llm | StrOutputParser()\n",
        "\n",
        "        # Invoke the chain and get the answer\n",
        "        answer = chain.invoke({\"context\": docs, \"chat_history\": self.memory})\n",
        "        return answer\n",
        "\n",
        "\n",
        "# Initialize the QueryProcessor with the PDF file (done once)\n",
        "file_path = \"FARM5CROPS_farmergpt.pdf\"\n",
        "query_processor = QueryProcessor(file_path)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "dt7rIDwDdonc",
        "outputId": "b469060d-d49e-4ea0-e1dc-623da71e8e38"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Answer: Here are some sugarcane varieties from different states in India:\n",
            "\n",
            "1. **Andhra Pradesh**:\n",
            "   - Early varieties: Co.6907, 84A125, 81A99, 83A30, 85A261, 87A298, Co.8014, 86V96, 91V83.\n",
            "   - Mid-late varieties: COA7607, CO8021, COT.8201, Co7805, COV92102 (83V15), 83V288.\n",
            "   - Late varieties: Co.7219, CoR8001, 87A380, Co7706.\n",
            "\n",
            "2. **Bihar**:\n",
            "   - Bo 99, CoP 9301, CoSe 98231, CoS 8436, Cos 95255, Bo 102, Bo 91, Bo 110, CoP 9206, CoSe 95422, CoSe 92423, UP 9530.\n",
            "\n",
            "3. **Gujarat**:\n",
            "   - Co 86002, Co 86032, CoSi 95071, Co 86249, CoN 05072.\n",
            "\n",
            "4. **Haryana**:\n",
            "   - CoJ 64, CoS 8436, CoS 88230, CoS 767.\n",
            "\n",
            "5. **Karnataka**:\n",
            "   - Co 94012, CoC 671, Co 92020, Co 8014, Co 86032, Co 62175, Co 8371, Co 740, Co 8011.\n",
            "\n",
            "6. **Maharashtra**:\n",
            "   - CoC 671, Co 86032, Co 8011, Co 94012, CoM 265, Co 92005.\n",
            "\n",
            "7. **Odisha**:\n",
            "   - Co 62175, CoA 89085, Co 87A298, Co86V96.\n",
            "\n",
            "8. **Punjab**:\n",
            "   - CoJ 85, CoJ 88, CoS8436, CoH 119, Co89003.\n",
            "\n",
            "9. **Tamil Nadu**:\n",
            "   - Co 94012, Co 94010, CoC 24.\n",
            "\n",
            "10. **Uttar Pradesh**:\n",
            "    - CoS 8436, Coj 64, CoS88230, CoS 98231, CoS 767, CoS 8432, CoPt 90223, CoS 92423, CoS97264, CoLk 8102.\n",
            "\n",
            "11. **Uttrakhand**:\n",
            "    - CoS 8436, CoS 88230, Cos 767, CoS 97264, CoSe 92423.\n",
            "\n",
            "These varieties are adapted to the specific climatic and soil conditions of their respective states.\n"
          ]
        }
      ],
      "source": [
        "query = \"give me some sugarcane variety names?\"\n",
        "answer = query_processor.get_answer(query)\n",
        "print(\"Answer:\", answer)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "lGUJMdVodorC",
        "outputId": "14b7cec0-c2e7-4675-914a-2ca412236461"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Answer: Based on the provided content, sugarcane is a significant crop in India, grown in both tropical and subtropical regions. However, the document does not specifically address crops that grow in dry regions outside of sugarcane. It does mention that sugarcane is grown in areas with varying climates, including semi-arid regions, which are typically dry. In these areas, sugarcane cultivation is supported by irrigation and specific planting methods to manage water efficiently.\n",
            "\n",
            "For a more comprehensive list of crops suitable for dry regions, additional information beyond the provided content would be necessary. Typically, crops like millet, sorghum, and certain legumes are known to be more drought-resistant and are often grown in dry regions.\n"
          ]
        }
      ],
      "source": [
        "# Reuse with another query\n",
        "query2 = \"What crops grow in dry regions?\"\n",
        "answer2 = query_processor.get_answer(query2)\n",
        "print(\"Answer:\", answer2)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1dpAeJtLnL4D"
      },
      "source": [
        "# General template for your chat with pdf\n",
        "##### change the promts as per requirements"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 614
        },
        "id": "RVc-wh3ld_Ga",
        "outputId": "dca7a49a-6589-45ec-b81e-312a4d6f85ab"
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "Colab notebook detected. This cell will run indefinitely so that you can see errors and logs. To turn off, set debug=False in launch().\n",
            "* Running on public URL: https://82384a2b41c5d0935c.gradio.live\n",
            "\n",
            "This share link expires in 1 week. For free permanent hosting and GPU upgrades, run `gradio deploy` from the terminal in the working directory to deploy to Hugging Face Spaces (https://huggingface.co/spaces)\n"
          ]
        },
        {
          "data": {
            "text/html": [
              "<div><iframe src=\"https://82384a2b41c5d0935c.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        }
      ],
      "source": [
        "# gradio app\n",
        "\n",
        "import gradio as gr\n",
        "from lancedb.rerankers import LinearCombinationReranker\n",
        "from langchain_community.document_loaders import PyPDFLoader\n",
        "from langchain_community.vectorstores import LanceDB\n",
        "from langchain_openai import OpenAIEmbeddings, ChatOpenAI\n",
        "from langchain_text_splitters import CharacterTextSplitter\n",
        "from langchain_core.prompts import PromptTemplate\n",
        "from langchain_core.output_parsers import StrOutputParser\n",
        "from langchain.memory import ConversationBufferMemory\n",
        "\n",
        "\n",
        "class QueryProcessor:\n",
        "    def __init__(self, file_path, db_url=\"lancedb_temp\", table_name=\"lancedb_indic\"):\n",
        "        loader = PyPDFLoader(file_path)\n",
        "        documents = loader.load()\n",
        "        text_splitter = CharacterTextSplitter()\n",
        "        self.documents = text_splitter.split_documents(documents)\n",
        "\n",
        "        embeddings = OpenAIEmbeddings()\n",
        "        self.vector_store = LanceDB(\n",
        "            uri=db_url, embedding=embeddings, table_name=table_name\n",
        "        )\n",
        "\n",
        "        self.reranker = LinearCombinationReranker(weight=0.3)\n",
        "        self.docsearch = LanceDB.from_documents(\n",
        "            self.documents, embeddings, reranker=self.reranker\n",
        "        )\n",
        "\n",
        "        self.llm = ChatOpenAI(\n",
        "            model_name=\"gpt-4o\",\n",
        "            temperature=0.01,\n",
        "        )\n",
        "        self.memory = ConversationBufferMemory(memory_key=\"chat_history\")\n",
        "\n",
        "    def generate_prompt_template(\n",
        "        self, main_instructions, prompt_instructions, context_name, query\n",
        "    ):\n",
        "        template = f\"\"\"{main_instructions}\n",
        "\n",
        "        {prompt_instructions}\n",
        "\n",
        "        {context_name}:\n",
        "        {{context}}\n",
        "\n",
        "        Previous Conversations:\n",
        "        {{chat_history}}\n",
        "        Human: {query}\n",
        "        Chatbot:\"\"\"\n",
        "        return PromptTemplate(\n",
        "            template=template, input_variables=[\"context\", \"chat_history\"]\n",
        "        )\n",
        "\n",
        "    def get_answer(self, query):\n",
        "        docs = self.docsearch.similarity_search_with_relevance_scores(query)\n",
        "\n",
        "        prompt = self.generate_prompt_template(\n",
        "            main_instructions=\"Act as a knowledgeable assistant. Answer the query comprehensively and concisely based on the provided content.\",\n",
        "            prompt_instructions=(\n",
        "                \"Focus on extracting the most relevant and accurate information from the context. \"\n",
        "                \"Prioritize clarity, conciseness, and detail in your response. \"\n",
        "                \"When summarizing, ensure key points are highlighted without losing important nuances. \"\n",
        "                \"If the context is insufficient to fully address the query, acknowledge the limitation clearly.\"\n",
        "            ),\n",
        "            context_name=\"Search Results\",\n",
        "            query=query,\n",
        "        )\n",
        "\n",
        "        chain = prompt | self.llm | StrOutputParser()\n",
        "\n",
        "        answer = chain.invoke({\"context\": docs, \"chat_history\": self.memory})\n",
        "        return answer\n",
        "\n",
        "\n",
        "def initialize_processor(pdf_file):\n",
        "    global query_processor\n",
        "    query_processor = QueryProcessor(pdf_file.name)\n",
        "    return \"PDF successfully loaded and processed. You can now ask questions.\"\n",
        "\n",
        "\n",
        "def query_processor_fn(question):\n",
        "    global query_processor\n",
        "    if query_processor is None:\n",
        "        return \"Please upload a PDF first.\"\n",
        "    return query_processor.get_answer(question)\n",
        "\n",
        "\n",
        "query_processor = None\n",
        "\n",
        "# Define Gradio interface\n",
        "with gr.Blocks() as app:\n",
        "    gr.Markdown(\"# RAG On PDF - FarmersGPT\")\n",
        "\n",
        "    with gr.Row():\n",
        "        pdf_upload = gr.File(label=\"Upload PDF\", file_types=[\".pdf\"])\n",
        "        pdf_status = gr.Textbox(label=\"Status\", interactive=False)\n",
        "\n",
        "    load_pdf_btn = gr.Button(\"Load PDF\")\n",
        "\n",
        "    with gr.Row():\n",
        "        user_query = gr.Textbox(\n",
        "            label=\"Ask a question\", placeholder=\"Enter your question here...\"\n",
        "        )\n",
        "        answer_box = gr.Textbox(label=\"Answer\", interactive=False)\n",
        "\n",
        "    ask_question_btn = gr.Button(\"Get Answer\")\n",
        "\n",
        "    load_pdf_btn.click(initialize_processor, inputs=[pdf_upload], outputs=[pdf_status])\n",
        "    ask_question_btn.click(\n",
        "        query_processor_fn, inputs=[user_query], outputs=[answer_box]\n",
        "    )\n",
        "\n",
        "app.launch(debug=True, share=True)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "id": "lWPuyELzxiRq"
      },
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "gpuType": "T4",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
